/**
 * @TODO Author 苟俊 jndx_taibai@163.com
 * Date  @2013-5-30
 * Copyright 2013 www.daoxila.com. All rights reserved.
 */
package com.flying.carclub.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flying.carclub.R;


/**
 * @author zhuxiaocheng
 *
 */
public class InfoBar extends RelativeLayout {

    private static final int DEFAULT_TITLE_STYLE = R.style.text_16_333333;
    private static final int DEFAULT_NUM_STYLE = R.style.text_16_ff608e;
    private Context mContext;

    private TextView mTitleText;
    private String nTitleStr;
    private int nTitleStyleId;

    private TextView mValueText;
    private String nValueStr;
    private int nValueStyleId;
    private int showMaxLines;

    private ImageView mIconView;
    private Drawable mIconDrawable;

    // 'New' 标签图标
    protected TextView mNewLabel;
    private TextView mNumLabel;
    private int nNumStyleId;

    private boolean bShowDividerView;
    private boolean bShowTopLine;
    private boolean bShowBottomLine;
    private View mDivider;
    private View mTopLine;
    private View mBottomLine;

    private View mLeftArraw, mRightArraw;
    private boolean bShowLeftArraw;

    public InfoBar(Context context) {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     */
    public InfoBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initFromAttr(context, attrs);
        initView(context);
    }

    private void initFromAttr(Context context, AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.widget_infobar, this);
        mIconView = (ImageView) findViewById(R.id.infobar_icon);
        mNewLabel = (TextView) findViewById(R.id.new_msg_icon);
        mNumLabel = (TextView) findViewById(R.id.num);
        mTitleText = (TextView) findViewById(R.id.title);
        mValueText = (TextView) findViewById(R.id.value);
        mDivider = findViewById(R.id.devider);
        mTopLine = findViewById(R.id.top_line);
        mBottomLine = findViewById(R.id.bottom_line);
        mLeftArraw = findViewById(R.id.icon_left_arrow);
        mRightArraw = findViewById(R.id.icon_right_arrow);


        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DxlInfoBar);
            nTitleStr = a.getString(R.styleable.DxlInfoBar_title_name);
            nTitleStyleId = a.getResourceId(R.styleable.DxlInfoBar_title_name_appearance, DEFAULT_TITLE_STYLE);
            mIconDrawable = a.getDrawable(R.styleable.DxlInfoBar_info_bar_icon);
            nValueStr = a.getString(R.styleable.DxlInfoBar_value_name);
            nValueStyleId = a.getResourceId(R.styleable.DxlInfoBar_value_name_appearance, DEFAULT_TITLE_STYLE);
            bShowDividerView = a.getBoolean(R.styleable.DxlInfoBar_show_divider, false);
            bShowTopLine = a.getBoolean(R.styleable.DxlInfoBar_show_top_line, false);
            bShowBottomLine = a.getBoolean(R.styleable.DxlInfoBar_show_bottom_line, false);
            bShowLeftArraw = a.getBoolean(R.styleable.DxlInfoBar_show_left_arraw, false);
            nNumStyleId = a.getResourceId(R.styleable.DxlInfoBar_num_appearance, DEFAULT_NUM_STYLE);
            showMaxLines = a.getInt(R.styleable.DxlInfoBar_show_max_lines, 1);
        } else {
            nTitleStyleId = DEFAULT_TITLE_STYLE;
            nValueStyleId = DEFAULT_TITLE_STYLE;
            nNumStyleId = DEFAULT_NUM_STYLE;
        }
    }

    private void initView(Context context) {
        //		setBackgroundResource(R.drawable.bg_comm_item_selector);
        mTitleText.setText(nTitleStr);
        mTitleText.setTextAppearance(context, nTitleStyleId);
        mNumLabel.setTextAppearance(context, nNumStyleId);
        if (showMaxLines > 0) {
            mTitleText.setMaxLines(showMaxLines);
        }

        if (!TextUtils.isEmpty(nValueStr)) {
            mValueText.setVisibility(View.VISIBLE);
            mValueText.setText(nValueStr);
        } else {
            mValueText.setVisibility(View.GONE);
        }
        mValueText.setTextAppearance(context, nValueStyleId);

        if (mIconDrawable != null) {
            mIconView.setVisibility(View.VISIBLE);
            //			mIconView.setBackgroundDrawable(mIconDrawable);
            mIconView.setImageDrawable(mIconDrawable);
        } else {
            mIconView.setVisibility(View.GONE);
        }
        if (bShowDividerView) {
            mDivider.setVisibility(View.VISIBLE);
        } else {
            mDivider.setVisibility(View.GONE);
        }
        if (bShowTopLine) {
            mTopLine.setVisibility(View.VISIBLE);
        } else {
            mTopLine.setVisibility(View.GONE);
        }
        if (bShowBottomLine) {
            mBottomLine.setVisibility(View.VISIBLE);
        } else {
            mBottomLine.setVisibility(View.GONE);
        }
        if (bShowLeftArraw) {
            mLeftArraw.setVisibility(View.VISIBLE);
        } else {
            mLeftArraw.setVisibility(View.GONE);
        }
    }

    public void showDivider() {
        mDivider.setVisibility(View.VISIBLE);
    }

    public void hideDivider() {
        mDivider.setVisibility(View.GONE);
    }

    public void showTopline() {
        mTopLine.setVisibility(View.VISIBLE);
    }

    public void hideTopline() {
        mTopLine.setVisibility(View.GONE);
    }

    public void showBottomLine() {
        mBottomLine.setVisibility(View.VISIBLE);
    }

    public void hideBottomLine() {
        mBottomLine.setVisibility(View.GONE);
    }

    /**
     * 显示标题左侧图标
     */
    public void showLabelIcon() {
        if (mIconView != null) {
            mIconView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏标题左侧图标
     */
    public void hideLabelIcon() {
        if (mIconView != null) {
            mIconView.setVisibility(View.GONE);
        }
    }

    public void setTitleIcon(Drawable drawable) {
        if (drawable == null) {
            mIconView.setVisibility(View.INVISIBLE);
        } else {
            //			mIconView.setBackgroundDrawable(drawable);
            mIconView.setImageDrawable(drawable);
            mIconView.setVisibility(View.VISIBLE);
        }
    }

    public void setTitleIcon(int resId) {
        Drawable drawable = mContext.getResources().getDrawable(resId);
        setTitleIcon(drawable);
    }

    public void setTitleName(String str) {
        mTitleText.setText(str);
    }

    public void setTitleName(CharSequence str) {
        mTitleText.setText(str);
    }

    public void setTitleName(int res) {
        setTitleName(getResources().getString(res));
    }

    public void setTitleAppreance(int res) {
        mTitleText.setTextAppearance(mContext, res);
    }

    public void setValueName(CharSequence str) {
        if (!TextUtils.isEmpty(str)) {
            mValueText.setVisibility(View.VISIBLE);
            mValueText.setText(str);
        } else {
            mValueText.setVisibility(View.GONE);
        }
    }

    public void setValueName(int res) {
        if (res != -1) {
            mValueText.setVisibility(View.VISIBLE);
            setTitleName(getResources().getString(res));
        } else {
            mValueText.setVisibility(View.GONE);
        }
    }

    public void setValueAppreance(int res) {
        if (res != -1) {
            mValueText.setVisibility(View.VISIBLE);
            mValueText.setTextAppearance(mContext, res);
        } else {
            mValueText.setVisibility(View.GONE);
        }
    }

    public void setBackground(Drawable drawable) {
        setBackgroundDrawable(drawable);
    }

    public void setBackground(int resid) {
        setBackgroundResource(resid);
    }

    public void setNewLabel(String newlabel) {
        mNewLabel.setText(newlabel);
        showNewLabel();
    }

    public void setNewLabel(int resid) {
        mNewLabel.setText(resid);
        showNewLabel();
    }

    public void setNewLabelBg(int resid) {
        mNewLabel.setBackgroundResource(resid);
        showNewLabel();
    }

    public void showNewLabel() {
        mNewLabel.setVisibility(View.VISIBLE);
    }

    public void hideNewLabel() {
        mNewLabel.setVisibility(View.GONE);
    }

    public void setNumLabel(String newlabel) {
        mNumLabel.setText(newlabel);
        showNumLabel();
    }

    public void setNumLabel(int resid) {
        mNumLabel.setText(resid);
        showNumLabel();
    }

    public void showNumLabel() {
        mNumLabel.setVisibility(View.VISIBLE);
    }

    public void hideNumLabel() {
        mNumLabel.setVisibility(View.GONE);
    }

    public void showDownArraw() {
        mLeftArraw.setVisibility(View.VISIBLE);
        mLeftArraw.setBackgroundResource(R.drawable.icon_down_arraw_small);
    }

    public void showUpArraw() {
        mLeftArraw.setVisibility(View.VISIBLE);
        mLeftArraw.setBackgroundResource(R.drawable.icon_up_arraw_small);
    }

    public void hideLeftArraw() {
        mLeftArraw.setVisibility(View.GONE);
    }

    public void hideRightArraw() {
        mRightArraw.setVisibility(View.INVISIBLE);
    }
    public int getShowMaxLines() {
        return showMaxLines;
    }

    public void setShowMaxLines(int showMaxLines) {
        mTitleText.setMaxLines(showMaxLines);
    }

    public void setTitlePadding(int left, int top, int right, int bottom) {
        mTitleText.setPadding(left, top, right, bottom);
    }
}
